﻿using JewererBasic.Classes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic.BO
{
    public class ProductBO
    {
        static public bool SaveProduct(PRODUCT product)
        {
            using (MSQDBEntities db = new MSQDBEntities())
            {                                
                var query = db.PRODUCTs.Find(product.Id);
                if (query == null)
                {                                                    
                    // save product to database
                    db.PRODUCTs.Add(product);
                    try
                    {
                        db.SaveChanges();
                        return true;
                    }
                    catch (Exception e)
                    {
                        ErrorLog.Instance.InternalErrors.Add("Error Add New Product: " + e.Message);
                        return false;
                    }
                }                   
            }
            return false;
        }

        static public PRODUCT Find(string id)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = db.PRODUCTs.Find(id);
            if (query != null)
                return query;
            return null;
        }

        static public bool Update(PRODUCT product)
        {
            using (MSQDBEntities db = new MSQDBEntities())
            {
                // use transaction to prevent error on data.
                using (var scope = new TransactionScope())
                {
                    var query = db.PRODUCTs.Find(product.Id);
                    if (query != null)
                    {
                        try
                        {
                            #region Update
                            // save product
                            if(string.IsNullOrEmpty(product.Drawing)==false)
                                query.Drawing = product.Drawing;
                            query.IdGamma = product.IdGamma;
                            query.IdKind = product.IdKind;
                            query.IdType = product.IdType;
                            query.IdUnit = product.IdUnit;
                            query.MetalWeight = product.MetalWeight;
                            query.Size = product.Size;
                            query.TotalWeight = product.TotalWeight;
                            db.SaveChanges();

                            // Update stone
                            // delete all product_stone
                            var stones = (from d in db.PRODUCT_STONE
                                          where string.Equals(d.IdProduct, product.Id)
                                          select d).ToList();
                            for (int i = 0; i < stones.Count; i++)
                                db.PRODUCT_STONE.Remove(stones[i]);
                            db.SaveChanges();
                            // update or add new product_stone
                            foreach (var item in product.PRODUCT_STONE)
                            {
                                item.IdProduct = product.Id;
                                db.PRODUCT_STONE.Add(item);
                                db.SaveChanges();
                            }                       

                            // Update component
                            // remove all product_component
                            var coms = (from d in db.PRODUCT_COMPONENT
                                        where string.Equals(d.IdProduct, product.Id)
                                        select d).ToList();
                            for (int i = 0; i < coms.Count; i++)
                                db.PRODUCT_COMPONENT.Remove(coms[i]);
                            db.SaveChanges();
                            // update or add new product_component
                            foreach (var item in product.PRODUCT_COMPONENT)
                            {
                                item.IdProduct = product.Id;
                                db.PRODUCT_COMPONENT.Add(item);
                                db.SaveChanges();
                            }
                            // casting
                            // delete all casting
                            var castings = (from d in db.PRODUCT_MOULD
                                            where string.Equals(d.IdProduct, product.Id)
                                            select d).ToList();
                            for (int i = 0; i < castings.Count; i++)
                                db.PRODUCT_MOULD.Remove(castings[i]);
                            db.SaveChanges();
                            foreach (var item in product.PRODUCT_MOULD)
                            {
                                item.IdProduct = product.Id;
                                db.PRODUCT_MOULD.Add(item);
                                db.SaveChanges();
                            }
                            // process
                            // remove all product_process
                            var processes = (from d in db.PRODUCT_PROCESS
                                             where string.Equals(d.IdProduct, product.Id)
                                             select d).ToList();
                            for (int i = 0; i < processes.Count; i++)
                            {
                                db.PRODUCT_PROCESS.Remove(processes[i]);
                            }
                            db.SaveChanges();
                            // update or add new product_process
                            foreach (var item in product.PRODUCT_PROCESS)
                            {                                
                                item.IdProduct = product.Id;
                                db.PRODUCT_PROCESS.Add(item);
                                db.SaveChanges();
                            }
                            #endregion
                            scope.Complete();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            ErrorLog.Instance.InternalErrors.Add("Update Fail: " + ex.Message);
                            return false;
                        }
                    }
                }
            }
            return false;
        }

        static public bool IsExisted(string nameItem)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = db.PRODUCTs.Find(nameItem);
            if (query != null)
                return true;
            return false;
        }        
    }
}